$(function () {
    $('#chat').keydown(function (event) {
        if (event.keyCode == 13) {
            // 回车事件

            var content = $(this).val();
            $.post(http_url + '/?s=index/chat/index', {'content':content, 'game_id':1, 'user_id': 10}, function (json) {
                if (json.code != 200) {
                    alert(json.message);
                    return;
                }
                //

            }, 'json');
        }
    });
})
var chat_ws_url = 'ws://140.143.228.54:82';

var chat_ws = new WebSocket(chat_ws_url);
chat_ws.onopen=function (evt) {
    console.log('chat ws connect success');
    chat_ws.send('send to server');
};

chat_ws.onmessage=function (evt) {
    var data = JSON.parse(evt.data);
    console.log(data);
    if (data.method == 'chatPush') {
        buildChat(data.data);
    }

};

chat_ws.onclose=function (evt) {
    console.log('chat ws connect has closed');
};

function buildChat(obj) {
    var html = '<div class="comment">' +'<span>'+obj.userName+'</span>' +'<span>'+obj.content+'</span>' +'</div>';
    $('#comments').append(html);
    // $('#comments > div:last-child').before(html);

}